<?php

namespace Home\Model;

use Think\Model;

class CartModel extends Model {
    //获取购物车内的商品详情
    function get_goods($store_id = 0,$user_id=0,$rec_ids=''){
        $carts = array();

        $cart_items = $this
                ->field(C('DB_PREFIX') . 'cart.*,' . C('DB_PREFIX') . 'store.store_name,' . C('DB_PREFIX') . 'store.enable_free_fee,' . C('DB_PREFIX') . 'store.amount_for_free_fee,' . C('DB_PREFIX') . 'store.acount_for_free_fee,' . C('DB_PREFIX') . 'goods_spec.stock')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store  ON ' . C('DB_PREFIX') . 'store.store_id = ' . C('DB_PREFIX') . 'cart.store_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods_spec  ON ' . C('DB_PREFIX') . 'goods_spec.spec_id = ' . C('DB_PREFIX') . 'cart.spec_id')
                ->where(($store_id?(C('DB_PREFIX') . 'cart.store_id = ' . $store_id ):(C('DB_PREFIX') . 'cart.store_id <> 0 ' )) . ' AND ' . C('DB_PREFIX') . 'cart.user_id=' . $user_id.($rec_ids?' AND rec_id IN('.$rec_ids.')':''))
                ->select();

        if (empty($cart_items)) {
            return $carts;
        }
        foreach ($cart_items as $item) {
            if(!isset($carts[$item['store_id']])){
                $carts[$item['store_id']]=array(
                    'amount'=>0,
                    'quantity'=>0,
                    'kinds'=>0,
                );
            }
			 /* 更新购物车商品信息start */
            $spec_info = M('goods_spec')->field(C('DB_PREFIX').'goods_spec.*,'.C('DB_PREFIX').'goods.goods_id,'.C('DB_PREFIX').'goods.store_id,'.C('DB_PREFIX').'goods.goods_name,'.C('DB_PREFIX').'goods.spec_qty,'.C('DB_PREFIX').'goods.spec_name_1,'.C('DB_PREFIX').'goods.spec_name_2,'.C('DB_PREFIX').'goods.spec_name_3,'.C('DB_PREFIX').'goods.spec_name_4')
                     ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods  ON ' . C('DB_PREFIX') . 'goods.goods_id = ' . C('DB_PREFIX') . 'goods_spec.goods_id')
                    ->where(C('DB_PREFIX').'goods_spec.spec_id='.$item['spec_id'])->find();
            if($spec_info){
                $item2=array();
                $spec_info = $this->get_spec_price($spec_info,$item['user_id'],(($item['promotions_id'] && ($item['goods_type']==GOODS_TYPE_GROUP))?$item['promotions_id']:0));
                $spec_1 = $spec_info['spec_name_1'] ? $spec_info['spec_name_1'] . ':' . '<img src="'.str_replace('goodsspec/','goodsspec/spec_',$spec_info['spec_1']).'">' : $spec_info['spec_1'];
                $spec_2 = $spec_info['spec_name_2'] ? $spec_info['spec_name_2'] . ':' . $spec_info['spec_2'] : $spec_info['spec_2'];
				$spec_3 = $spec_info['spec_name_3'] ? $spec_info['spec_name_3'] . ':' . $spec_info['spec_3'] : $spec_info['spec_3'];
				$spec_4 = $spec_info['spec_name_4'] ? $spec_info['spec_name_4'] . ':' . $spec_info['spec_4'] : $spec_info['spec_4'];
				 if(strpos($spec_1,'img') !== false){ 
                     preg_match_all('/src="(.*)"/iUs', $spec_1, $out);
                $img=$out[1][0];
                $item['goods_image'] = $img;
                 };
                ($spec_info['spec_qty']>0) && $specification = $spec_1 . ' ' . $spec_2 . ' ' . $spec_3 . ' ' . $spec_4;
                 $item['price']=$item2['price']=$spec_info['price'];
                        $item['promotions_id']=$item2['promotions_id'] = !empty($spec_info['promotions_id']) ? $spec_info['promotions_id']:0;
                        $item['goods_old_price']=$item2['goods_old_price'] = $spec_info['goods_old_price'];
                        $item['goods_type']=$item2['goods_type'] = $spec_info['goods_type'];
                        $item['specification']=$item2['specification'] = (trim($specification));
                        $item['goods_name']=$item2['goods_name'] = addslashes($spec_info['goods_name']);
                        $this->where('rec_id=' . '"' . $item['rec_id'] . '"')->save($item2);
            }
            /* 更新购物车商品信息 end */
            /* 小计 */
            $item['subtotal'] = $item['price'] * $item['quantity'];
            /* 以店铺ID为索引 */
//            empty($item['goods_image']) && $item['goods_image'] = Conf::get('default_goods_image');
            $carts[$item['store_id']]['store_id'] = $item['store_id'];
            $carts[$item['store_id']]['store_name'] = $item['store_name'];
            $carts[$item['store_id']]['enable_free_fee'] = $item['enable_free_fee'];
            $carts[$item['store_id']]['amount_for_free_fee'] = $item['amount_for_free_fee'];
            $carts[$item['store_id']]['acount_for_free_fee'] = $item['acount_for_free_fee'];
            $carts[$item['store_id']]['amount'] += $item['subtotal'];   //各店铺的总金额
            $carts[$item['store_id']]['quantity'] += $item['quantity'];   //各店铺的总数量
            $carts[$item['store_id']]['kinds'] ++; 
            $carts[$item['store_id']]['items'][] = $item;
        }
        return $carts;
    }
    //此处用来统一处理 价格 当用户同时购买产品的时候  用来选择 哪一个价格
    function get_spec_price($spec_info,$user_id,$promotions_id=0) {
        
        //选择最小的价格
        $min_price = $spec_info['price'];
        
        $spec_info['goods_old_price'] = $spec_info['price'];
        $spec_info['goods_type'] = GOODS_TYPE_NORMAL;
        

        /* 读取促销价格 */
        $promotion_price = D('Promotion')->get_promotion_price($spec_info['goods_id'], $spec_info['spec_id']);
        if ($promotion_price < $min_price) {
            $spec_info['promotions_id'] = M('Promotion')->where("start_time<=" . time() . " AND end_time>=" . time() . " AND goods_id=" . $spec_info['goods_id'])->getField('pro_id');
            $spec_info['goods_type'] = GOODS_TYPE_PRO;
            $min_price = $promotion_price;
        }

        //把原价改为会员所获得的优惠价
        if ($spec_info['if_open'] == 1) {
            $discount = D('Gradegoods')->get_user_discount($user_id, $spec_info['goods_id']);
            if ($discount > 0) {
                $discount_price = round($spec_info['price'] * $discount, 2);
                //判断取最低价
                if ($discount_price < $min_price) {
                    $spec_info['goods_type'] = GOODS_TYPE_VIP;
                    $min_price = $discount_price;
                }
            }
        }
        $spec_info['price']  = $min_price;
        
        if ($promotions_id) {
            $groupbuy = M('ju')->where('status=1 AND goods_id=' . $spec_info['goods_id'])->find();
            if ($groupbuy) {
                $spec_price = unserialize($groupbuy['spec_price']);
                if ($spec_price[$spec_info['spec_id']]['price'] && $spec_info['price'] > $spec_price[$spec_info['spec_id']]['price']) {
                    $min_price = $spec_price[$spec_info['spec_id']]['price'];
                    $spec_info['goods_type'] = GOODS_TYPE_GROUP;
                    $spec_info['price'] = $min_price;
                    $spec_info['max_per_user'] = $groupbuy['max_per_user'];
                    $spec_info['promotions_id'] = $groupbuy['group_id'];
                }
            }
        }
        return $spec_info;
    }
}

?>
